#include "comm.hpp"

//由server端来创建和删除共享的文件
int main()
{
    bool r = createFifo(NAME_PIPE);
    assert(r);

    int rfd = open(NAME_PIPE, O_RDONLY);
    //读端打开后会阻塞在这里，不会向后继续运行，等写端也打开才会继续向后运行
    if(rfd < 0)
        cout << strerror(errno) << endl;

    char buffer[1024];
    while(1)
    {
        ssize_t n = read(rfd, buffer, sizeof(buffer)-1);
        if(n > 0)
        {
            buffer[n] = 0;
            cout << "client -> server: " << buffer << endl;
        }
        //读取到0时说写入端退出了，因为其他情况下读入数据或者阻塞，只有读到0时是写入端退出
        else if(n == 0)
        {
            cout << "client quit! me too!" << endl;
            break;
        }
        else
        {
            cout << strerror(errno) << endl;
            break;
        }
    }

    // sleep(10);
    close(rfd);

    removeFifo(NAME_PIPE);
    return 0;
}
